package com.chenfan.finance.config.sharding;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liran
 */
@Component
public abstract class CommonShardDataBase {

    public static final String DB_SHARD_TIME_FORMAT = "yyyyMM";

    public final static String DB_MAPPING_CONFIG = "alipay:1,alipay:2";
    public static Map<String, String> db = new HashMap<>();

    @PostConstruct
    public void initDb() {
        String[] split = DB_MAPPING_CONFIG.split(",");
        for (String s : split) {
            String[] split1 = s.split(":");
            db.put(split1[1], split[0]);
        }
    }

    /**
     * 根据shop id 获取db name
     *
     * @param shopId
     * @return
     */
    public String getDatabaseByShopId(String shopId) {
        String s = db.get(shopId);
        return s;
    }
}
